package com.lanou3g.musicenjoy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.musicenjoy.entity.Music;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class MusicAdapter extends BaseAdapter{


    private Context mContext;
    private ImageLoader imageLoader;
    private List<Music.SonglistEntity> datas;


    public  MusicAdapter(Context c){
        mContext=c;
        imageLoader= VolleySingleton.getInstance(mContext).getImageLoader();
    }
    public void initData(List<Music.SonglistEntity> datas){
        if (this.datas==null){
            this.datas=new ArrayList<Music.SonglistEntity>();
        }else {
            this.datas.clear();
        }
        this.datas=datas;
        notifyDataSetChanged();

    }
    @Override
    public int getCount() {
        return datas!=null&&datas.size()>0?datas.size():0;
    }

    @Override
    public Object getItem(int position) {
        return datas!=null&&datas.size()>0?datas.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if(convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.fragment_music_item,null);
            viewHolder=new ViewHolder();
            viewHolder.view= (TextView) convertView.findViewById(R.id.itemTextView);
            viewHolder.textView= (TextView) convertView.findViewById(R.id.musicTextView);

            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        Music.SonglistEntity infor= (Music.SonglistEntity) getItem(position);
//        ImageLoader.ImageListener imageListener=ImageLoader.getImageListener(viewHolder.imageView,
//                R.mipmap.background,R.mipmap.background);
//        imageLoader.get(infor.getAvatar_big(), imageListener);
        viewHolder.view.setText(infor.getTitle());
        viewHolder.textView.setText(infor.getAuthor());



        return convertView;
    }
    private class ViewHolder{
        private TextView view;
        private TextView textView;

    }
}
